package simulateOS.entity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class File {
    private String fileName;/*记录文件名*/
    private int flag;/*判定为什么类型的文件,0为可读,1为可读且可写*/
    private int length;/*记录文件盘块的长度(占据多少盘块)*/
    private String content;/*记录文件内的内容*/
    private String type;/*记录文件的类型*/
    private int diskNum;/*记录当前文件的起始磁盘盘块号*/
    private String location;/*记录文件的地址信息*/
    private int size;/*记录文件大小*/
    private String space;/*记录文件的大小显示0.0字节*/
    private Date createTime;/*记录文件创建时间*/
    private Folder parent;/*记录当前文件的父文件夹（若为根目录则置为null）*/
    private boolean isOpen;/*记录文件是否已打开*/
    private List<Integer> point = new ArrayList<>();/*直接记录文件分配到的盘块下标*/
    //偷懒的可执行文件内容
    private boolean isExecutableFile = false;/*记录是否为可执行文件*/
    private List<String> list = new ArrayList<>();/*将文件内的语句拆分为行存储*/

    public File(String fileName,String location,int diskNum,Folder parent){
        this.fileName = fileName;
        this.location = location;
        this.diskNum = diskNum;
        this.parent = parent;
        this.type = "文件";
        this.length = 1;
        this.content = null;
        this.setSize();
        this.createTime = new Date();
        this.setOpen(false);
        this.setExecutableFile(false);
        if (this.hasParent()) {
            Folder parent1 = this.getParent();
            parent1.setSize(parent1.getFolderSize(parent1));
            while(parent1.hasParent()) {
                parent1 = parent1.getParent();
                parent1.setSize(parent1.getFolderSize(parent1));
            }
        }
    }

    private boolean hasParent() {
        return this.parent != null;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getDiskNum() {
        return diskNum;
    }

    public void setDiskNum(int diskNum) {
        this.diskNum = diskNum;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Folder getParent() {
        return parent;
    }

    public void setParent(Folder parent) {
        this.parent = parent;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public int getSize() {
        return size;
    }

    public void setSize() {
        if(getList().isEmpty()){
            this.size = 0;
        }else {
            this.size = getList().size();
        }
        this.setSpace(this.size + "B");
    }

    public String getSpace() {
        return space;
    }

    public void setSpace(String space) {
        this.space = space;
    }

    public String getCreateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
        return format.format(this.createTime);
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public boolean isOpen() {
        return isOpen;
    }

    public void setOpen(boolean open) {
        isOpen = open;
    }

    public List<Integer> getPoint() {
        return point;
    }

    public void setPoint(List<Integer> point) {
        this.point = point;
    }

    public boolean isExecutableFile() {
        return isExecutableFile;
    }

    public void setExecutableFile(boolean executableFile) {
        isExecutableFile = executableFile;
    }

    public String toString(){
        return fileName;
    }

    public String toName(){
        return fileName;
    }

    //重载文件返回的指令汇总，同时更新文件的size
    public void initList(){
        if(getContent() == null){
            return;
        }
        list.clear();
        String sp = "\n";
        String[] TEXTS = getContent().split(sp);
        if(TEXTS.length > 0){
            for (String s:TEXTS) {
                list.add(s);
            }
        }
        setSize();
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }
}
